//
//  YSHotPostsView.m
//  Yueshi
//
//  Created by yueshi－mac1 on 15/12/4.
//  Copyright © 2015年 悦食中国. All rights reserved.
//

#import "YSHotPostsView.h"
#define kMarginLeft 12
#define kDefaultHeight 34

#define kNumFont 8
#define kOvertopImage 2
#define kLeftPadding 3
#define kTopPadding 1

@interface YSHotPostsView()

@property (strong, nonatomic)  UILabel *postNameLabel;
@property (strong, nonatomic)  UIImageView *commentImage;
@property (strong, nonatomic)  UILabel *numLabel;
@property (strong, nonatomic)  UIView *lineView;
@end

@implementation YSHotPostsView


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {

        UILabel *postNameLabel = [[UILabel alloc] init];
        [postNameLabel setFont:SYS_FONT(14)];
        [postNameLabel setTextColor:HEXColor(@"#999491")];
        [self addSubview:postNameLabel];
        _postNameLabel = postNameLabel;
        
        _commentImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_message_gray_s"]];
        [self addSubview:_commentImage];
        
        _numLabel = [[UILabel alloc]init];
        [_numLabel setTextColor:[UIColor whiteColor]];
        [_numLabel setFont:SYS_FONT(kNumFont)];
        [_numLabel setTextAlignment:NSTextAlignmentCenter];
        [_numLabel setHidden:YES];
        
        _numLabel.layer.masksToBounds = YES;
        [_numLabel.layer setCornerRadius:0];
        [_numLabel.layer setBorderColor:RGBColor(204, 204, 204).CGColor];
        [_numLabel.layer setBackgroundColor:[ThemeColor CGColor]];
        [self addSubview:_numLabel];
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = HEXColor(@"e5e5e5");
        [self addSubview:lineView];
        _lineView = lineView;
        
        UITapGestureRecognizer*tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClick:)];
        
        [self addGestureRecognizer:tapGesture];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    NSLog(@"YSHotPostsView =============== layoutSubviews");
    NSLog(@"%f = %f",self.width ,self.height);
    
    CGSize imageSize = _commentImage.image.size;
    CGSize nameSize = [_groupReview.postName sizeWithFont:SYS_FONT(14)];
    self.postNameLabel.frame = CGRectMake(0, self.height - 5 - nameSize.height, self.width - imageSize.width - 10, nameSize.height);
    
    _commentImage.frame = CGRectMake(self.width - imageSize.width - 5, self.height - 5 - imageSize.height, imageSize.width, imageSize.height);
    
    
    [self setNum:_groupReview.commentNum];
    
    _lineView.frame = CGRectMake(0, self.height - 1, self.width, 1);
    
}

- (void)setGroupReview:(YSGroupReview *)groupReview{
    _groupReview = groupReview;
    
    [_postNameLabel setText:groupReview.postName];
    [self setNum:groupReview.commentNum];
}


- (void)setNum:(NSInteger)num{
    if (num <=0) {
        [_numLabel setHidden:YES];
        return;
    }
    
    NSString *numStr = [NSString stringWithFormat:@"%lu",(unsigned long)num];
    
    [_numLabel setHidden:NO];
    if(num>999){
        numStr = @"999+";
    }
    
    CGSize size = [numStr sizeWithFont:SYS_FONT(kNumFont)];
    CGFloat numWidth = size.width +kLeftPadding*2;
    CGFloat numHeight = size.height + kTopPadding *2;
    
    if (num<10) {
        numWidth = numHeight;
    }
    
    [_numLabel.layer setCornerRadius:numHeight*0.5];
    
    [_numLabel setFrame:CGRectMake(CGRectGetMaxX(_commentImage.frame) -(numWidth)*0.5, CGRectGetMinY(_commentImage.frame) - kOvertopImage, numWidth, numHeight)];
    
    [_numLabel setText:numStr];
}

- (void)onClick:(id)sender {
    if ([_delegate respondsToSelector:@selector(hotPostsClickWithHotPostsView:)]) {
        [_delegate hotPostsClickWithHotPostsView:self];
    }
}


@end
